/**
 *
 */
package com.yufei.infoExtractor.util;

import java.math.BigDecimal;



/**
 * created by @author jasstion at 2012-9-25
 * 用于字符串的处理（截取，替换，内容编码的转换等等）
 */
public class StringUtil {

	public static String ascii2Native(String str) {
	
		StringBuilder sb = new StringBuilder();
	
		int begin = 0;
	
		int index = str.indexOf(CommonUtil.PREFIX);
	
		while (index != -1) {
	
			sb.append(str.substring(begin, index));
	
			sb.append(StringUtil.ascii2Char(str.substring(index, index + 6)));
	
			begin = index + 6;
	
			index = str.indexOf(CommonUtil.PREFIX, begin);
	
		}
	
		sb.append(str.substring(begin));
	
		return sb.toString();
	
	}

	static char ascii2Char(String str) {
	
		if (str.length() != 6) {
	
			throw new IllegalArgumentException(
	
			"Ascii string of a native character must be 6 character.");
	
		}
	
		if (!CommonUtil.PREFIX.equals(str.substring(0, 2))) {
	
			throw new IllegalArgumentException(
	
			"Ascii string of a native character must start with \"\\u\".");
	
		}
	
		String tmp = str.substring(2, 4);
	
		int code = Integer.parseInt(tmp, 16) << 8;
	
		tmp = str.substring(4, 6);
	
		code += Integer.parseInt(tmp, 16);
	
		return (char) code;
	}
	  private static String decodeUnicode(final String dataStr) {
	        int start = 0;
	        int end = 0;
	        final StringBuffer buffer = new StringBuffer();
	        while (start > -1) {
	            int system = 10;//进制
	            if(start==0){
	                int t = dataStr.indexOf("&#");
	                if(start!=t)start = t;
	            }
	            end = dataStr.indexOf(";", start + 2);
	            String charStr = "";
	            if (end != -1) {
	                charStr = dataStr.substring(start + 2, end);

	                //判断进制
	                char s = charStr.charAt(0);
	                if(s=='x' || s=='X'){
	                    system = 16;
	                    charStr = charStr.substring(1);
	                }
	            }
	            //转换
	            try{
	                char letter = (char) Integer.parseInt(charStr,system);
	                buffer.append(new Character(letter).toString());
	            }catch(NumberFormatException e){
	                e.printStackTrace();
	            }

	            //处理当前unicode字符到下一个unicode字符之间的非unicode字符
	            start = dataStr.indexOf("&#",end);
	            if(start-end>1){
	                buffer.append(dataStr.substring(end+1, start));
	            }

	            //处理最后面的非unicode字符
	            if(start==-1){
	                int length = dataStr.length();
	                if(end+1!=length){
	                    buffer.append(dataStr.substring(end+1,length));
	                }
	            }
	        }
	        return buffer.toString();
	    }


public static void main(String[] args){
	
}
//四舍五入
public static int getInt(Float value){
	return new BigDecimal(value).setScale(0, BigDecimal.ROUND_UP).intValue();
}
}
